//
//  SYUserViewController.m
//  BSHOUYIFANG
//
//  Created by lanou3g on 15/8/20.
//  Copyright (c) 2015年 CYB. All rights reserved.
//

#import "SYUserViewController.h"
#import "Header.h"
#import <QuartzCore/QuartzCore.h>
#import <MobileCoreServices/MobileCoreServices.h>
#import <quartzcore/CoreAnimation.h>
#import "SYFileHandle.h"
#import "SYMyFmdb.h"
#import "SYChangeNickNameViewController.h"
#import "SYSChangePasswordViewController.h"
@interface SYUserViewController ()<UITableViewDataSource,UITableViewDelegate,UIImagePickerControllerDelegate>
@property (nonatomic,strong)UITableView *userTableView;
@property (nonatomic,strong)NSArray *titleArray;
@end

@implementation SYUserViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self navigationItemTitleColor];
    // Do any additional setup after loading the view.
    _userTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStyleGrouped];
    _userTableView.dataSource = self;
    _userTableView.delegate = self;
    [_userTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    _titleArray =@[@"头像",@"昵称",@"修改密码",@"退出登录"];
    UIButton*leftButton = [[UIButton alloc]initWithFrame:CGRectMake(0,0,30*0.6,30*0.6)];
    [leftButton setImage:[UIImage imageNamed:@"iconfont-fanhui"]forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(searchprogram) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem*leftItem = [[UIBarButtonItem alloc]initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem= leftItem;
    [self.view addSubview:_userTableView];
}

- (void)searchprogram{
    [self.navigationController popToRootViewControllerAnimated:YES];
}


- (void)navigationItemTitleColor
{
    UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    [customLab setTextColor:[UIColor whiteColor]];
    [customLab setText:@"我的信息"];
    customLab.textAlignment = NSTextAlignmentCenter;
    customLab.font = [UIFont boldSystemFontOfSize:20];
    self.navigationItem.titleView = customLab;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _titleArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.textLabel.text =_titleArray[indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    if (indexPath.row == 0) {
        UIImage *image = [UIImage imageWithData:[[SYFileHandle shareInstance] icon]];
        UIImageView *icon = [[UIImageView alloc]initWithImage:image];
        icon.frame = CGRectMake(0, 0, 50, 50);
        icon.layer.cornerRadius = 25;
        icon.layer.masksToBounds = YES;
        cell.accessoryView = icon;
    }
    if (indexPath.row == 1) {
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 15)];
        label.text = [[SYFileHandle shareInstance] username];
        label.textAlignment = NSTextAlignmentRight;
        cell.accessoryView = label;
    }
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 60;
    }
    return 40;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        [self createAlert];
    }
    if (indexPath.row == 1) {
        SYChangeNickNameViewController *changeVC = [[SYChangeNickNameViewController alloc]init];
        changeVC.holder = [[SYFileHandle shareInstance] username];
        [self.navigationController pushViewController:changeVC animated:YES];
    }
    if (indexPath.row == 2) {
        SYSChangePasswordViewController *changeVC = [[SYSChangePasswordViewController alloc]init];
        [self.navigationController pushViewController:changeVC animated:YES];
    }
    if (indexPath.row == 3) {
        [[SYFileHandle shareInstance] setloginState:NO];
        [[SYFileHandle shareInstance] clearAll];
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"退出成功" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
        [alert show];
        [alert dismissWithClickedButtonIndex:0 animated:YES];
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
}


- (void)createAlert{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *photoAction = [UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [self getMediaFromSource:UIImagePickerControllerSourceTypePhotoLibrary];
    }];
    UIAlertAction *cremaAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [self getMediaFromSource:UIImagePickerControllerSourceTypeCamera];
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:cremaAction];
    [alert addAction:cancelAction];
    [alert addAction:photoAction];
    [self presentViewController:alert animated:YES completion:nil];

}



- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    UITableViewCell *cell = [_userTableView cellForRowAtIndexPath:indexPath];
    UIImageView *imageView = (UIImageView *)cell.accessoryView;
    imageView.image = image;
    NSData *imageData = UIImagePNGRepresentation(image);
    [[SYFileHandle shareInstance] setIcon:imageData];
    SYUser *user = [[SYFileHandle shareInstance] user];
    user.icon = imageData;
    [[SYMyFmdb shareInstance] updataWithUser:user];
    [picker dismissViewControllerAnimated:YES completion:^{
        
    }];
}


- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:^{
        
    }];
}



-(void)getMediaFromSource:(UIImagePickerControllerSourceType)sourceType
{
    NSArray *mediatypes = [UIImagePickerController availableMediaTypesForSourceType:sourceType];
    
    if([UIImagePickerController isSourceTypeAvailable:sourceType] &&[mediatypes count]>0)
    {
        NSArray *mediatypes = [UIImagePickerController availableMediaTypesForSourceType:sourceType];
        
        UIImagePickerController *picker=[[UIImagePickerController alloc] init];
        
        picker.mediaTypes = mediatypes;
        
        picker.delegate = self;
        
        picker.allowsEditing = YES;
        
        picker.sourceType = sourceType;
        
        NSString *requiredmediatype = (NSString *)kUTTypeImage;
        
        NSArray *arrmediatypes = [NSArray arrayWithObject:requiredmediatype];
        
        [picker setMediaTypes:arrmediatypes];
        
        [self presentViewController:picker animated:YES completion:^{
            
        }];
    }
    else
    {
        //如果不支持拍摄功能就弹出提示
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"错误信息!" message:@"当前设备不支持拍摄功能" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles: nil];
        [alert show];
    }
}


- (void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.translucent = NO;
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated{
    self.tabBarController.tabBar.translucent = YES;
    self.tabBarController.tabBar.hidden = NO;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
